<template lang="html">
  <section class="bar-wrapper">
    <div class="chart-warp">
      <div class="chart-title">Levels of Interest</div>
      <div id="dashboard-stats" class="chart bars-horizontal brand-primary">
        <!-- row -->
        <div class="row">
          <span class="label">Interest 1</span>
          <div class="bar-wrap">
            <div class="bar" data-value="62"></div>
          </div>
          <span class="number">62</span>
        </div>
        <!-- row -->
        <div class="row">
          <span class="label">Interest 2</span>
          <div class="bar-wrap">
            <div class="bar" data-value="12"></div>
          </div>
          <span class="number">12</span>
        </div>
        <!-- row -->
        <div class="row">
          <span class="label">Interest 3</span>
          <div class="bar-wrap">
            <div class="bar" data-value="33"></div>
          </div>
          <span class="number">33</span>
        </div>
        <!-- row -->
        <div class="row">
          <span class="label">Interest 4</span>
          <div class="bar-wrap">
            <div class="bar" data-value="81"></div>
          </div>
          <span class="number">81</span>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
export default {
  methods: {
    init () {
      function generateBarGraph (wrapper) {
        let values = [];
        let bars = document.querySelectorAll(wrapper + ' .bar')
        bars.forEach((item, index) => {
          values.push(item.getAttribute("data-value"))
        })
        let max_value = Math.max.apply(Math, values)

        bars.forEach((item, index) => {
          let bar = item,
            value = bar.getAttribute('data-value'),
            percent = Math.ceil((value / max_value) * 100)

          bar.style.width = `${percent}%`
          bar.className += ' in'
        })
      }
      setTimeout(() => {
        generateBarGraph('#dashboard-stats')
      })
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
section,
div,
span {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
.bar-wrapper {
  font-family: 'Open Sans', sans-serif;
  color: #4e555f;
  font-size: 14px;
}

@mixin barDelays ($index, $moredelay, $amount) {
  @if $index < ($amount + 1)  {
    $multi: $index * 200ms;
    $delay: $multi + $moredelay;

    .row:nth-of-type(#{$index}) {
      .bar {
        transition: 1s $delay;
      }
    }
    @include barDelays($index+1, $moredelay, $amount);
  }
}

$label-width: 150px;
$number-width: 30px;
$bar-height: 26px;

.chart-warp {
  max-width: 900px;
  margin: 0 auto;
  padding: 10px;
  border: 1px solid #eeeeee;
  .chart-title {
    margin-bottom: 10px;
    font-size: 16px;
    text-align: center;
    text-transform: uppercase;
  }
  .row {
    position: relative;
    height: $bar-height;
    margin-bottom: 15px;
  }
  .bar-wrap {
    position: relative;
    background: rgb(215, 219, 222);
    width: 100%;
    height: 100%;
    overflow: hidden;
    .bar {
      position: absolute;
      top: 0;
      left: 0;
      width: 0%;
      height: 100%;
      background: rgb(243, 113, 103);
      transition: all 1s;
      transform: translateX(-100%);
      &.in {
        transform: translateX(0%);
      }
    }
  }

  @include barDelays(1, 200ms, 7);

  .label {
    position: absolute;
    top: 0;
    left: 0;
    width: $label-width;
    padding-left: 10px;
    text-align: left;
    font-size: 12px;
    line-height: $bar-height;
    text-transform: uppercase;
    font-weight: bold;
    z-index: 10;
  }
  .number {
    position: absolute;
    top: 0;
    right: 0;
    width: $number-width;
    padding-right: 10px;
    font-size: 18px;
    line-height: $bar-height;
    font-weight: bold;
    text-align: right;
    z-index: 10;
  }
  @media screen and (min-width: 800px) {
    .row {
      padding: 0 ($number-width + 10px) 0 ($label-width + 10px);
    }
    .label {
      text-align: right;
      padding-left: 0;
    }
    .number {
      text-align: left;
      padding-right: 0;
    }
  }
}
</style>
